import subprocess
import IPy

def ip_check(ipadress):
    try:
        ips = IPy.IP(ipadress)
        try:
            fp = open('ip_result.txt', 'w',encoding='utf-8')
            fp.seek(0)
            for ip in ips:
                if subprocess.run('ping -c 10 -W 10' + str(ip)):
                    fp.write(f'{ip}已被使用\n')
                else:
                    fp.write(f'{ip}未被使用\n')
        except FileExistsError as fe:
            print('文件错误或不存在')
    except ValueError as ve:
        print('输入的ip有误（字段/掩码）')

ip = input('请输入你想查询的ip字段，掩码：')
ip_check(ip)
